package com.easy.core.dbHandle.imp;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import com.easy.core.dbHandle.CommonDao;
import com.easy.core.dbHandle.CommonService;
import com.easy.core.dbHandle.PaginationSupport;

/**
 * <p>Description: </p>
 * <p>CreateDate:@2013-8-23</p>
 * @author zhangj
 */
public class CommonServiceImp implements CommonService {
	
	public <K> K findById(Class<K> clazz, Serializable id) {
		return commonDao.findById(clazz, id);
	}
	
	@SuppressWarnings("unchecked")
	public <K> K findById(String entityName, Serializable id) {
		return (K) commonDao.findById(entityName, id);
	}

	public <K> List<K> findAll(Class<K> clazz) {
		return commonDao.findAll(clazz);
	}
	
	public <K> List<K> findAll(String entityName) {
		return commonDao.findAll(entityName);
	}

	public <K> List<K> findByCriteria(DetachedCriteria dc) {
		return commonDao.findByCriteria(dc);
	}

	public <K> List<K> findTopByCriteria(DetachedCriteria dc, int num) {
		return commonDao.findTopByCriteria(dc, num);
	}
	
	public Long findCountByCriteria(final DetachedCriteria dc){
		return commonDao.findCountByCriteria(dc);
	}

	public <K> PaginationSupport<K> findPageByCriteria(DetachedCriteria dc, int pageSize, int startIndex, List<Order> orderList) {
		return commonDao.findPageByCriteria(dc, pageSize, startIndex, orderList);
	}

	public <K> PaginationSupport<K> findPageByCriteria(DetachedCriteria dc, int pageSize, int startIndex, Order order) {
		List<Order> orderList = new ArrayList<Order>();
		orderList.add(order);
		return commonDao.findPageByCriteria(dc, pageSize, startIndex, orderList);
	}
	
	public List<?> findByHQL(String hql, Object... params) {
		return commonDao.findByHQL(hql, params);
	}

	public Serializable save(Object object) {
		return commonDao.save(object);
	}

	public Serializable save(String entityName, Object object) {
		return commonDao.save(entityName, object);
	}

	public void update(Object object) {
		commonDao.update(object);
	}

	public void update(String entityName, Object object) {
		commonDao.update(entityName, object);
	}

	public void delete(Object object) {
		commonDao.delete(object);
	}
	
	public Session getCurrentSession(boolean allowCreate){
		return commonDao.getCurrentSession(allowCreate);
	}
	
	public SessionFactory getSessionFactory(){
		return commonDao.getSessionFactory();
	}

	public CommonDao getCommonDao() {
		return commonDao;
	}

	public void setCommonDao(CommonDao commonDao) {
		this.commonDao = commonDao;
	}
	
	protected CommonDao commonDao;

}
